package servlets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ImageServlet
 */
public class ImageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private String filePath;   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ImageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
	@Override
	public void init() throws ServletException {
		filePath = getServletContext().getInitParameter("subdirArchivos"); 
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
	    if(filename == null || filename.isEmpty() || filename.equals("/.png"))
	    	filename = "default.png";
		File file = new File(filePath, filename);

	    response.setContentType(getServletContext().getMimeType(file.getName()));
	    response.setContentLength((int) file.length());
	    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

	    BufferedInputStream input = null;
	    BufferedOutputStream output = null;

	    try {
	        input = new BufferedInputStream(new FileInputStream(file));
	        output = new BufferedOutputStream(response.getOutputStream());

	        byte[] buffer = new byte[8192];
	        int length;
	        while ((length = input.read(buffer)) > 0) {
	            output.write(buffer, 0, length);
	        }
	    } finally {
	        if (output != null) try { output.close(); } catch (IOException ignore) {}
	        if (input != null) try { input.close(); } catch (IOException ignore) {}
	    }

	}

}
